package com.lyy.common.sms;

import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component("ALiYunSMSUtils")
public class ALiYunSMSUtils implements SendSMS {

	@Override
	public boolean sendSMS(Map<String, String> paramMap) {
		DefaultProfile profile = DefaultProfile.getProfile("default", ALiYunSMSModel.accessKeyId, ALiYunSMSModel.accessSecret);
		IAcsClient client = new DefaultAcsClient(profile);
		CommonRequest request = new CommonRequest();
		request.setMethod(MethodType.POST);
		request.setDomain("dysmsapi.aliyuncs.com");
		request.setVersion("2017-05-25");//信息版本，固定的不能改
		request.setAction("SendSms");
		//必填:待发送手机号。支持以逗号分隔的形式进行批量调用，
		//批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式；
		//发送国际/港澳台消息时，接收号码格式为国际区号+号码，如“85200000000” 
		request.putQueryParameter("PhoneNumbers", (String) paramMap.get("PhoneNumbers"));
		request.putQueryParameter("SignName", (String) paramMap.get("SignName"));
		request.putQueryParameter("TemplateCode", (String) paramMap.get("TemplateCode"));
		request.putQueryParameter("TemplateParam", (String) paramMap.get("TemplateParam"));
		try {
			CommonResponse response = client.getCommonResponse(request);
			//System.out.println(response.getData());
			JSONObject json = JSONObject.parseObject(response.getData());
			String code = json.getString("Code");
			if ("OK".equals(code)) {
				return true;
			}
		} catch (ClientException e) {
			e.printStackTrace();
		}
		return false;
	}
	
}